// utils/device.js
export function getDeviceId() {
  // 本地先读
  let did = plus.storage.getItem('_did');
  if (did && did !== 'null' && did !== 'undefined' && did.trim() !== '') {
    return did;
  }

  // 没有就生成
  did = plus.device.uuid;          // 原生层 UUID
  
  // 确保 device.uuid 有效
  if (!did || did === 'null' || did === 'undefined' || did.trim() === '') {
    // 如果 device.uuid 无效，生成一个基于时间戳的临时ID
    did = 'temp_' + Date.now() + '_' + Math.random().toString(36).substr(2, 9);
  }
  
  plus.storage.setItem('_did', did);
  return did;
}